package com.eascs.web.o2o.web.controller;


import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.scheduler.common.entity.ScheduleJob;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.AjaxButton;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.RedirectButton;
import com.eascs.tag.MenuTag;
import com.eascs.web.o2o.web.common.BaseMasterViewController;
import com.eascs.web.o2o.web.form.ScheduleJobForm;
import com.eascs.web.o2o.web.service.QuartzService;

@Controller
@RequestMapping("/quartz")
public class QuartzController extends BaseMasterViewController{
	
	@Inject
	QuartzService quartzService;
	
	
	@Override
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "quartz");
	}
	
	@RequestMapping("/jobList")
	public String  getScheduleJobList(ScheduleJobForm vo){
		String o2ourl = "/quartz/jobList";
		if(vo != null){
			String jobName = vo.getJobName() == null ? "" : vo.getJobName();
			o2ourl = UrlUtil.appendUrl(o2ourl, "jobName", jobName);
			o2ourl = o2ourl + "&pageIndex=";
		}else{
			o2ourl = o2ourl + "?pageIndex=";
		}
		vo.setPageSize(10);
		PageResult<Object[]> result = quartzService.findScheduleJobInfo(vo);
		String titles[] = {"任务名称","类注解名称","方法名称","CRON表达式","状态","更新时间","上次执行时间","上次结束时间","优先级","描述"};
		
		RedirectButton button1 = new RedirectButton("  修改", "/quartz/updateJob?jobId=");
		AjaxButton button2 = new AjaxButton("  运行#暂停","/rest_quartz/useJob?jobId=");
		AjaxButton button3 = new AjaxButton("  移除","/rest_quartz/deleteJob?jobId=");
		Button[] detailButton = new Button[] { button1, button2,button3};
		
		PaginationInfo paginationInfo = new PaginationInfo(result, titles, o2ourl);
		paginationInfo.setFirstRenderColumnIndex(1);
		paginationInfo.setBuildPagingNavigation(true);
		paginationInfo.setMoreButtons(detailButton);
		ViewContext.put("paginationInfo", paginationInfo);
		ViewContext.put("vo", vo);
		BaseRestResult.refresh();
		return "quartz/quartz_list";
	}
	
	@RequestMapping(value = "/addJob")
	public String addJob(ScheduleJobForm vo) {
		ViewContext.put("flag", "add");
		return "quartz/quartz_detail";
	}
	
	@RequestMapping(value = "/updateJob")
	public String updateJob(ScheduleJobForm vo) {
		ScheduleJob job = quartzService.getJobById(vo.getJobId());
		vo.setJobName(job.getJobName());
		vo.setClassPath(job.getClassPath());
		vo.setMethod(job.getMethod());
		vo.setCronExpression(job.getCronExpression());
		vo.setPriority(job.getPriority());
		vo.setRemark(job.getRemark());
		ViewContext.put("vo", vo);
		ViewContext.put("flag", "update");
		return "quartz/quartz_detail";
	}
	
	
	
}
